import java.util.Scanner;

/**
 * @author gao
 * @date 2025/7/20 9:47
 */

public class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        // 用于存储每一天的状态
        int[][] dp = new int[n][3];
        // 第一天持有股票的状态
        dp[0][0] = -prices[0];
        for (int i = 1; i < n; i++) {
            // 更新第i天的状态，即第i天持有股票的状态
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
            // 更新第i天的状态，即第i天不持有股票的状态
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][2]);
            // 更新第i天的状态，即第i天卖出股票的状态
            dp[i][2] = dp[i - 1][0] + prices[i];
        }
        // 返回最后一天的最大利润
        return Math.max(dp[n - 1][1], dp[n - 1][2]);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int[] prices = new int[n];
            for (int i = 0; i < prices.length; i++) {
                prices[i] = in.nextInt();
            }
            Solution solution = new Solution();
            System.out.println(solution.maxProfit(prices));
        }
    }
}